package com.mrd.weixinpay.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.mrd.frame.domain.Order;
import com.mrd.frame.domain.TradeStatement;
import com.mrd.frame.domain.message.ReconciliationMessage;
import com.mrd.frame.domain.message.Statement;
import com.mrd.frame.domain.params.PayParams;
import com.mrd.frame.domain.params.RefundParams;

public class BeanUtil {
	/**
	 * 将流水单转换为对账请求返回信息对象
	 * @author dusy
	 * @param tradeStatement
	 * @return
	 */
	public static Statement changeToStatement(TradeStatement tradeStatement){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Statement statement = new Statement();
		if(tradeStatement != null){
			statement.setTradeNo(tradeStatement.getTradeNo());
			statement.setRelatedTradeNo(tradeStatement.getRelatedTradeNo());
			statement.setTradeType(tradeStatement.getTradeType());
			statement.setOrderId(tradeStatement.getOrderId());
			statement.setPayChannel(tradeStatement.getPayChannel());
			statement.setTotalAmount(tradeStatement.getTotalAmount()+"");
			statement.setTradeTime(sdf.format(tradeStatement.getTradeTime()));
		}
		return statement;
	}
	
	public static ReconciliationMessage changeToReconciliationMessage(List<TradeStatement> tradeStatementList){
		ReconciliationMessage rm = new ReconciliationMessage();
		List<Statement> statementList = new ArrayList<Statement>();
		if(tradeStatementList != null){
			for(TradeStatement ts : tradeStatementList){
				statementList.add(changeToStatement(ts));
			}
		}
		rm.setStatementList(statementList);
		return rm;
	}
	/**
	 * 传入参数转换为对象
	 * @param params 仅为支付和退款参数
	 * @return
	 */
	public static Order paramsToOrder(Object params){
		Order order = new Order();
		if(params != null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			order.setOrderId(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date()) + PayCommonUtil.buildRandom(5));
			order.setStatus("1");//请求参数转换为订单，默认订单状态进行中
			order.setPayType("NATIVE");//暂时只有扫码支付
			if(params instanceof PayParams){
				PayParams payParams = (PayParams)params;
				order.setPayChannel(payParams.getPayChannel());
				order.setTradeNo(payParams.getOutTradeNo());
				try {
					order.setTimeStart(sdf.parse(payParams.getTimeStart()));
				} catch (ParseException e1) {
					order.setTimeStart(new Date());
				}
				try {
					order.setTotalAmount(Integer.parseInt(payParams.getTotalAmount()));
				} catch (Exception e) {
					order.setTotalAmount(-1);//金额传入不正确
				}
				order.setTradeType("1");
				order.setUserKey(payParams.getUserKey());
			}else if(params instanceof RefundParams){
				RefundParams refundParams = (RefundParams)params;
				order.setPayChannel(refundParams.getPayChannel());
				order.setTradeNo(refundParams.getOutRefundNo());
				order.setRelatedTradeNo(refundParams.getOutTradeNo());
				try {
					order.setTimeStart(sdf.parse(refundParams.getTimeStart()));
				} catch (ParseException e1) {
					order.setTimeStart(new Date());
				}
				order.setTotalAmount(refundParams.getTotalAmount());
				order.setTradeType("2");
				order.setUserKey(refundParams.getUserKey());
			}else{
				return null;
			}
		} 
		return order;
	}
}
